package cn.sdhuijin.healthexam.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import cn.sdhuijin.healthexam.ExitApplication;
import cn.sdhuijin.healthexam.R;
import cn.sdhuijin.healthexam.control.AbstractControlActivity;
import cn.sdhuijin.healthexam.util.Util;

/**
 * 搜索
 * 
 * 修改完成
 * 
 * @author sunping
 * 
 */
public class SearchInfoActivity extends AbstractControlActivity implements
		OnClickListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search_info);
		ExitApplication.getInstance().addActivity(this);

		setEditTextListener(R.id.edt_birth_date);
		setEditTextListener(R.id.edt_begin_date);
		setEditTextListener(R.id.edt_end_date);
		this.setButtonListener(this, R.id.all_search_btn, R.id.btn_back);
	}

	/**
	 * 拼接查询sql语句
	 * 
	 * @return
	 */
	public String getSearchSql() {
		StringBuffer sql = new StringBuffer(
				"SELECT t2.姓名 AS 姓名, t2.身份证号 AS 身份证号,t2.性别 AS 性别,t2.出生日期 AS 出生日期,t1.表格类型 AS 类型 ,t1.医务人员 AS 医生,t1.录入日期 AS 录入日期 FROM 索引表 t1 LEFT JOIN 身份证表 t2 ON t1.身份证号 = t2.身份证号  WHERE ");

		String name = this.getControlValue(R.id.edt_name);
		if (Util.allIsNotEmpty(name)) {
			sql.append("t2.姓名  = '" + name + "' AND ");
		}
		String carfId = this.getControlValue(R.id.edt_cardId);
		if (Util.allIsNotEmpty(carfId)) {
			sql.append("t2.身份证号  = '" + carfId + "' AND ");
		}
		boolean male = this.isCheckBoxChecked(R.id.search_sex_male);
		boolean female = this.isCheckBoxChecked(R.id.search_sex_female);
		if (male && female) {

		} else if (male || female) {
			sql.append("t2.性别 in (");
			if (male) {
				sql.append("'男',");
			}
			if (female) {
				sql.append("'女',");
			}
			if ((sql.length() > 0) && (',' == sql.charAt(sql.length() - 1)))
				sql = sql.deleteCharAt(sql.length() - 1);
			sql.append(") AND ");
		}
		String birthDate = this.getControlValue(R.id.edt_birth_date);
		if (Util.allIsNotEmpty(birthDate)) {
			sql.append("t2.出生日期= '" + birthDate + "' AND ");
		}
		String doctor = this.getControlValue(R.id.edt_doctor);
		if (Util.allIsNotEmpty(doctor)) {
			sql.append("t1.医务人员= '" + doctor + "' AND ");
		}

		String beginDate = this.getControlValue(R.id.edt_begin_date); // 获得开始日期
		String endDate = this.getControlValue(R.id.edt_end_date); // 获得结束日期

		if (Util.allIsNotEmpty(beginDate, endDate)) {
			beginDate = Util.dateFormat(beginDate) + "000000";
			endDate = Util.dateFormat(endDate) + "000000";
			sql.append("t1.录入日期  >= '" + beginDate + "' AND ");
			sql.append("t1.录入日期  < '" + endDate + "' AND ");
		}
		boolean archivesChecked = this
				.isCheckBoxChecked(R.id.all_search_archives_lab);
		boolean examinationChecked = this
				.isCheckBoxChecked(R.id.all_search_examination_lab);
		boolean jsbVisitChecked = this
				.isCheckBoxChecked(R.id.search_visit_jsb_lab);
		boolean gxyVisitChecked = this
				.isCheckBoxChecked(R.id.search_visit_gxy_lab);
		boolean tnbVisitChecked = this
				.isCheckBoxChecked(R.id.search_visit_tnb_lab);
		// 中医体质判定
		boolean bodyJudgeVisitChecked = this
				.isCheckBoxChecked(R.id.search_body_judge_lab);
		// 产前第一次随访
		boolean chanQianFirstVisitChecked = this
				.isCheckBoxChecked(R.id.search_chanqianfirst_lab);
		// 产前第二次随访
		boolean chanQianSecondVisitChecked = this
				.isCheckBoxChecked(R.id.search_chanqiansecond_lab);
		// 产前第三次随访
		boolean chanQianThirdVisitChecked = this
				.isCheckBoxChecked(R.id.search_chanqianthird_lab);
		// 产前第四次随访
		boolean chanQianForthVisitChecked = this
				.isCheckBoxChecked(R.id.search_chanqianforth_lab);
		// 产前第五次随访
		boolean chanQianFifthVisitChecked = this
				.isCheckBoxChecked(R.id.search_chanqianfifth_lab);
		// 产后随访
		boolean chanHouVisitChecked = this
				.isCheckBoxChecked(R.id.search_chanhou_lab);
		// 产后四十二天随访
		boolean chanHouSiShiErVisitChecked = this
				.isCheckBoxChecked(R.id.search_chanhou_sishier_lab);
		// 新生儿随访
		boolean NewChildVisitChecked = this
				.isCheckBoxChecked(R.id.search_xinshenger_lab);
		// 新生儿满月随访
		boolean NewChildManYueVisitChecked = this
				.isCheckBoxChecked(R.id.search_xinshenger_manyue_lab);
		// 新生儿三月龄随访
		boolean NewChildSanYueVisitChecked = this
				.isCheckBoxChecked(R.id.search_xinshenger_sanyue_lab);
		// 新生儿六月龄随访
		boolean NewChildLiuYueVisitChecked = this
				.isCheckBoxChecked(R.id.search_xinshenger_liuyue_lab);
		// 新生儿八月龄随访
		boolean NewChildBaYueVisitChecked = this
				.isCheckBoxChecked(R.id.search_xinshenger_bayue_lab);
		// 新生儿十二月随访
		boolean NewChildShiErYueVisitChecked = this
				.isCheckBoxChecked(R.id.search_xinshenger_shieryue_lab);
		// 新生儿十八月随访
		boolean NewChildShiBaYueVisitChecked = this
				.isCheckBoxChecked(R.id.search_xinshenger_shibayue_lab);
		// 新生儿二十四月随访
		boolean NewChildErShiSiYueVisitChecked = this
				.isCheckBoxChecked(R.id.search_xinshenger_ershisiyue_lab);
		// 新生儿三十月随访
		boolean NewChildSanShiYueVisitChecked = this
				.isCheckBoxChecked(R.id.search_xinshenger_sanshiyue_lab);
		// 新生儿三岁随访
		boolean NewChildSanSuiVisitChecked = this
				.isCheckBoxChecked(R.id.search_xinshenger_sansui_lab);
		// 新生儿四岁随访
		boolean NewChildSiSuiVisitChecked = this
				.isCheckBoxChecked(R.id.search_xinshenger_sisui_lab);
		// 新生儿五岁随访
		boolean NewChildWuSuiVisitChecked = this
				.isCheckBoxChecked(R.id.search_xinshenger_wusui_lab);
		// 新生儿六岁随访
		boolean NewChildLiuSuiVisitChecked = this
				.isCheckBoxChecked(R.id.search_xinshenger_liusui_lab);
		if (archivesChecked || examinationChecked || jsbVisitChecked
				|| gxyVisitChecked || tnbVisitChecked || bodyJudgeVisitChecked
				|| chanQianFirstVisitChecked || chanQianSecondVisitChecked
				|| chanQianThirdVisitChecked || chanQianForthVisitChecked
				|| chanQianFifthVisitChecked || chanHouVisitChecked
				|| chanHouSiShiErVisitChecked || NewChildVisitChecked
				|| NewChildManYueVisitChecked || NewChildSanYueVisitChecked
				|| NewChildLiuYueVisitChecked || NewChildBaYueVisitChecked
				|| NewChildShiErYueVisitChecked || NewChildShiBaYueVisitChecked
				|| NewChildErShiSiYueVisitChecked || NewChildSanSuiVisitChecked
				|| NewChildSiSuiVisitChecked || NewChildWuSuiVisitChecked
				|| NewChildLiuSuiVisitChecked) {
			sql.append("t1.表格类型 in (");
			if (archivesChecked) {
				sql.append("'档案',"); // 档案类型
			}
			if (examinationChecked) {
				sql.append("'体检',"); // 体检类型
			}
			if (jsbVisitChecked) {
				sql.append("'精神病随访',");
			}
			if (gxyVisitChecked) {
				sql.append("'高血压随访',");
			}
			if (tnbVisitChecked) {
				sql.append("'糖尿病随访',");
			}
			if (chanQianFirstVisitChecked) {
				sql.append("'产前第一次随访',");
			}
			if (chanQianSecondVisitChecked) {
				sql.append("'产前第二次随访',");
			}
			if (chanQianThirdVisitChecked) {
				sql.append("'产前第三次随访',");
			}
			if (chanQianForthVisitChecked) {
				sql.append("'产前第四次随访',");
			}
			if (chanQianFifthVisitChecked) {
				sql.append("'产前第五次随访',");
			}
			if (chanHouVisitChecked) {
				sql.append("'产后随访',");
			}
			if (chanHouSiShiErVisitChecked) {
				sql.append("'产后四十二天随访',");
			}
			if (NewChildVisitChecked) {
				sql.append("'新生儿随访',");
			}
			if (NewChildManYueVisitChecked) {
				sql.append("'新生儿满月随访',");
			}
			if (NewChildSanYueVisitChecked) {
				sql.append("'新生儿三月龄随访',");
			}
			if (NewChildLiuYueVisitChecked) {
				sql.append("'新生儿六月龄随访',");
			}
			if (NewChildBaYueVisitChecked) {
				sql.append("'新生儿八月龄随访',");
			}
			if (NewChildShiErYueVisitChecked) {
				sql.append("'新生儿十二月龄随访',");
			}
			if (NewChildShiBaYueVisitChecked) {
				sql.append("'新生儿十八月龄随访',");
			}
			if (NewChildErShiSiYueVisitChecked) {
				sql.append("'新生儿二十四月龄随访',");
			}
			if (NewChildSanShiYueVisitChecked) {
				sql.append("'新生儿三十月龄随访',");
			}
			if (NewChildSanSuiVisitChecked) {
				sql.append("'新生儿三岁龄随访',");
			}
			if (NewChildSiSuiVisitChecked) {
				sql.append("'新生儿四岁龄随访',");
			}
			if (NewChildWuSuiVisitChecked) {
				sql.append("'新生儿五岁龄随访',");
			}
			if (NewChildLiuSuiVisitChecked) {
				sql.append("'新生儿六岁龄随访',");
			}
			if (bodyJudgeVisitChecked) {
				sql.append("'中医体质判定',");
			}
			if ((sql.length() > 0) && (',' == sql.charAt(sql.length() - 1)))
				sql = sql.deleteCharAt(sql.length() - 1);
			sql.append(") AND ");
		}
		sql.append(" t1.是否完成 = '是'");
		String searchSql = new String(sql);
		return searchSql;
	}

	@Override
	public void onClick(View v) {
		int clickId = (Integer) v.getId();
		switch (clickId) {
		case R.id.all_search_btn:
			Intent intent = new Intent();
			intent.putExtra("sql", getSearchSql());
			Log.i("SQL语句", getSearchSql());
			intent.setClass(this, DataListActivity.class);
			startActivity(intent);
			break;
		case R.id.btn_back:
			finish();
			break;
		}
	}
}
